package com.lenovo.tool;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class TranslationRename
{
	private final int MODE_F = 0;
	private final int MODE_D = 1;
	private final int MODE_FD = 2;
	
	private Map<String, String> map;
	
	public static void main(String[] args)
	{
		if (args.length != 1)
		{
			System.out.println("please enter the config file name");
			return;
		}
		
		File configFile = new File(args[0]);
		if (!configFile.exists() || !configFile.isFile())
		{
			System.out.println("config file error");
			return;
		}
		
		TranslationRename rename = new TranslationRename();
		rename.renameFiles(configFile);
	}
	
	private void renameFiles(File configFile)
	{
		try
		{
			Document doc = DocumentUtils.openDocument(configFile);
			String basedirStr = ((Element)doc.getElementsByTagName("basedir").item(0)).getTextContent().trim();
			if(basedirStr.length() == 0)
			{
				System.out.println("basedir is null");
				return;
			}
			File basedir = new File(basedirStr);
			if(!basedir.isDirectory())
			{
				System.out.println("basedir is not a directionary");
				return;
			}
			String mode =  ((Element)doc.getElementsByTagName("type").item(0)).getTextContent().trim();
			if("f".equalsIgnoreCase(mode))
			{
				initRule(doc);
				rename(basedir, MODE_F);
			}
			else if("d".equalsIgnoreCase(mode))
			{
				initRule(doc);
				rename(basedir, MODE_D);
			}
			else if("fd".equalsIgnoreCase(mode))
			{
				initRule(doc);
				rename(basedir, MODE_FD);
			}
			else
			{
				System.out.println("type is null or wrong");
			}
		}  catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
	}

	private void initRule(Document doc)
	{
		NodeList nodes;
		Element item;
		
		map = new HashMap<String, String>();
		nodes = doc.getElementsByTagName("strings");
		for (int i = 0; i < nodes.getLength(); i++) {
			item = (Element) nodes.item(i);
			map.put(item.getTextContent(), item.getAttribute("target"));
		}
		
		nodes = doc.getElementsByTagName("arrays");
		for (int i = 0; i < nodes.getLength(); i++) {
			item = (Element) nodes.item(i);
			map.put(item.getTextContent(), item.getAttribute("target"));
		}
	}

	private void rename(File root, int mode)
	{
		File subFiles[] = root.listFiles();
		String src;
		String path;
		String target;
		for (int i = 0; i < subFiles.length; i++)
		{
			path = subFiles[i].getAbsolutePath().replace('\\', '/');
			src = path.substring(path.lastIndexOf('/') + 1);
			if (subFiles[i].isDirectory())
			{
				if(mode == MODE_D || mode == MODE_FD)
				{
					target = map.get(src);
					if(target != null)
					{
						subFiles[i].renameTo(new File(subFiles[i].getParentFile(), target));
					}
				}
				rename(subFiles[i], mode);
			} else
			{
				if(mode == MODE_F || mode == MODE_FD)
				{
					target = map.get(src);
					if(target != null)
					{
						subFiles[i].renameTo(new File(subFiles[i].getParentFile(),target));
					}
				}
			}
		}
	}
	
}
